<?php
//前端路由器
class DispathLib{
	function __construct(){
	}
	function authDispath(){
		if(URL_REWRITE_ON){
			//这里还需要考虑一下
		}else{
			$ctrl = _g(PARA_CTRL,'ctrl',0);
			$ac = _g(PARA_AC,'ac',0);
			$appid = _g(PARA_APP_ID,'int',1);
			$code = _g(PARA_CODE,null,1);
		}
		//验证此模块是否为开放状态
		$AppClass = get_instance_of('AppLib');
		$app_info = $AppClass->authApp($appid);
		if(!isset($app_info['ip']))
			stop('$app_info["ip"]'." is not defined" );
		
		if(!$app_info['ip'])
			stop('$app_info["ip"]'." is null" );
		
		if('all' !=$app_info['ip']){
			$ip = get_client_ip();
			$allow_ip = explode(",", $app_info['ip']);
			if(!in_array($ip, $allow_ip))
				stop('IP不允许访问' );
		}
		
		if(!isset($app_info['token']))
			stop('此APP无token','app');
		
		$auth_code = md5( $appid . $app_info['token']);
		if($auth_code != $code)
			stop('token_code验证失败','app');
		
		if(!$ctrl)
			if(defined('DEF_CTRL'))
				$ctrl = DEF_CTRL;
			else
				stop('ctrl参数为空','G_PARA');
		
		if(!$ac)
			if(defined('DEF_AC'))
				$ac = DEF_AC;
			else
				stop('ac参数为空','G_PARA');
			
		$dir =  BASE_DIR .DS. C_DIR_NAME . DS ;
		$ctrl_file = strtolower($dir . $ctrl .C_EXT);
		if( !file_exists($ctrl_file))
			stop('ctrl文件不存在','FILE');
		
		include_once $ctrl_file;
		if ( !class_exists($ctrl.C_CLASS))
			stop('ctrl类不存在','FILE');
		
		if(! method_exists($ctrl.C_CLASS,$ac))
			stop('ac方法不存在','FILE');
		
		$app_ctrl_info = $AppClass->authCtrl($appid,$ctrl,$ac);

		define('APP_ID',$appid);
		define("CTRL", $ctrl);
		define("AC", $ac);
		
		$this->appid = $appid;
		$this->ctrl = $ctrl;
		$this->ac = $ac;
		
	}
	
	function action(){
		$ac = $this->ac;
		$ctrl = $this->ctrl .C_CLASS;
		LogLib::accessWrite('access');
		$ctrlClass = get_instance_of($ctrl);
		$ctrlClass->$ac();
		
	}
	
	function rewrite($ctrl,$ac){
		
	}
}
